package assetservicelogic

import (
	"context"
	"github.com/zeromicro/go-zero/core/logx"
	"property_service/dao/model"
	"property_service/dao/query"
	"property_service/internal/svc"
	"property_service/property_service"
	"property_service/utils/BeanUtils"
	"property_service/utils/result"
)

type AssetPurchaseDetailAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewAssetPurchaseDetailAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AssetPurchaseDetailAddLogic {
	return &AssetPurchaseDetailAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// AssetPurchaseDetailAdd 创建采购明细表
func (l *AssetPurchaseDetailAddLogic) AssetPurchaseDetailAdd(in *property_service.AssetPurchaseDetail) (*property_service.Response, error) {
	// 获取采购明细表模型的查询构造器
	m := query.Use(l.svcCtx.Gorm).AssetPurchaseDetail
	// 创建一个新的采购明细表模型对象，不包含ID、UpdatedAt、CreatedAt字段
	mod := &model.AssetPurchaseDetail{
		RequestID:            in.RequestID,                                     // 申请单ID
		AssetID:              in.AssetID,                                       // 资产ID
		AssetName:            in.AssetName,                                     // 资产名称
		BrandModel:           in.BrandModel,                                    // 品牌型号
		BrandModelID:         in.BrandModelId,                                  //品牌id
		Quantity:             in.Quantity,                                      // 数量
		Unit:                 in.Unit,                                          // 单位
		ReferencePrice:       in.ReferencePrice,                                // 参考单价
		Amount:               in.Amount,                                        // 金额
		RequestReason:        in.RequestReason,                                 // 申请原因
		ExpectedDeliveryDate: BeanUtils.GetDateFormat(in.ExpectedDeliveryDate), // 期望到货日期
		TenantID:             in.TenantID,                                      // 租户ID
		CreatedAtuser:        in.CreatedAtuser,                                 // 创建用户
		AssetCode:            in.AssetCode,                                     //  资产编号
		HasArrived:           in.HasArrived,                                    //  已到数量(入库引用后对库存进行限制)
		StockID:              in.StockID,                                       //  仓库ID
	}
	// 使用上下文执行创建操作
	if err := m.WithContext(l.ctx).Create(mod); err != nil {
		// 如果创建失败，返回错误响应
		return result.Err(err).Ed()
	}
	// 创建成功，返回成功响应
	return result.Suc().AddEd()
}
